package com.songlin.testv2ex.data;

import android.util.Log;

import com.songlin.testv2ex.android_gson_json.HttpUtils;
import com.songlin.testv2ex.model.Member;
import com.songlin.testv2ex.model.Node;
import com.songlin.testv2ex.model.Topic;
import com.songlin.testv2ex.model.TopicsList;

import org.json.JSONArray;
import org.json.JSONObject;


/**
 * Created by 松林 on 2015/10/6.
 */
public class FileReadWrite {
    public void getTopicsList(final String url) {
        TopicsList topicsList = new TopicsList();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                TopicsList topicsList = new TopicsList();
                String data = HttpUtils.getJsonContent(url);    //从API获取数据
                try {
                    JSONArray jsonArray = new JSONArray(data);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        Topic topicTemp = new Topic();
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        topicTemp.id = jsonObject.getInt("id");
                        System.out.println(topicTemp.id);
                        topicTemp.title = jsonObject.getString("title");
                        topicTemp.url  = jsonObject.getString("url");
                        topicTemp.content = jsonObject.getString("content");
                        topicTemp.contentRendered = jsonObject.getString("content_rendered");
                        topicTemp.replies = jsonObject.getInt("replies");

                        JSONObject memberData = jsonObject.getJSONObject("member");
                        Member member = new Member();
                        member.id = memberData.getInt("id");
                        member.username = memberData.getString("username");
                        member.tagline = memberData.getString(("tagline"));
                        member.avatar = memberData.getString("avatar_normal");
                        topicTemp.member = member;

                        JSONObject nodeData = jsonObject.getJSONObject("node");
                        Node node = new Node();
                        node.id = nodeData.getInt("id");
                        node.name = nodeData.getString("name");
                        node.title = nodeData.getString("title");
                        node.titleAlternative = nodeData.getString("title_alternative");
                        node.url = nodeData.getString("url");
                        node.topics = nodeData.getInt("topics");
                        node.avatar = nodeData.getString("avatar_normal");
                        topicTemp.node = node;

                        topicTemp.created = jsonObject.getLong("created");
                        topicTemp.lastModified = jsonObject.getLong("last_modified");
                        topicTemp.lastTouched = jsonObject.getLong("last_touched");

                        topicsList.add(topicTemp);
                        Log.d("- - - - - - - - - -", " - - - Success - - - ");
                    }
                    /*  * 把topics的实例存入全局变量 *  */
                    GlobalData.Lastest_Topics = topicsList;
                } catch (Exception ex) {
                    Log.d("Throws Exception", "From \"public View onCreateView \" in class FirstFragment");
                    ex.printStackTrace();
                }
            }
        });
        thread.start();
    }
}
